*****************************************************************************
* Code for generating weather data at lhe year-county level
* 
* - Calls 
* - Generate baseline and counterfactual temperature
* 
*****************************************************************************

clear all
set more off
global path_metaData "Data_ori/Schlenker/metaData"
global path_rawData "Data_ori/Schlenker/rawDataByYear_v2020"
global path_code "Code/1a. CODE Weather data.do"

*****************************************************************************
* Load grid information
*****************************************************************************

use "$path_metaData/cropArea.dta", clear
gen longitude = -125 + mod(gridNumber-1,1405)/24
label var longitude "longitude of grid centroid (decimal degrees)"
gen latitude  = 49.9375+1/48 - ceil(gridNumber/1405)/24
label var latitude  "latitude of grid centroid (decimal degrees)"
merge 1:1 gridNumber using "$path_metaData/linkGridnumberFIPS.dta"
drop _merge
compress
sort gridNumber
save "Data_new/gridInfo.dta", replace

*****************************************************************************
* Set parameters in following section
*****************************************************************************
* Degree days bound list
global boundList 0 29

* Define growing season
* first month and day of month used in a year
global monthBegin = 4  
global dayMonthBegin =  1
* last month and day of month used in a year
global monthEnd   = 9 
global dayMonthEnd   = 30

*****************************************************************************
* Loop over years, generating data in baseline and counterfactual scenarios
*****************************************************************************
forvalues y = 1950(1)2019 {

	global yearMin = `y'
	global yearMax = `y'
	
	*** Baseline
	global save_name "Data_new/Weather_year`y'.dta"
	global counterC = 0
	do "$path_code"

	*** Counterfactual +1C
	global save_name "Data_new/Weather_year`y'_1C.dta"
	global counterC = 1
	do "$path_code"

}

*****************************************************************************
* Drop original data (to save space)
*****************************************************************************

forvalues y = 1950(1)2019 {
	shell rmdir "$path_rawData/year`y'" /s
}

*****************************************************************************
* Put together in a data base
*****************************************************************************

* Baseline weather
use "Data_new/Weather_year1950.dta", clear
forvalues y = 1951(1)2019 {
	append using "Data_new/Weather_year`y'.dta"
}
save "Data_ori/Schlenker/Weather_data_1950_2019.dta", replace

* Counterfactual weather +1C
use "Data_new/Weather_year1950_1C.dta", clear
forvalues y = 1951(1)2019 {
	append using "Data_new/Weather_year`y'_1C.dta"
}
save "Data_ori/Schlenker/Weather_data_1950_2019_1C.dta", replace

*****************************************************************************
* Drop yearly data sets 
*****************************************************************************

forvalues y = 1950(1)2019 {
	erase "Data_new/Weather_year`y'.dta"
	erase "Data_new/Weather_year`y'_1C.dta"
}

